module comm.QueueJob;

import std.stdio;
import core.thread;
import core.sync.condition;
import core.sync.mutex;
import std.container : DList;
import  message.MsgDefine;
import executor.message.UnrecevieMsgExecutor;

class QueueJob {

  private __gshared QueueJob _instance;

  private this()
  {
    _mutex = new Mutex();
    _condition = new Condition(_mutex);
  }

  static public QueueJob getInstance()
  {
    if (_instance is null)
    {
      _instance = new QueueJob();
    }
    return _instance;
  }

  void startJob()
  {
      do {
        _mutex.lock();
        while(_queue.empty())
        {
            _condition.wait();
        }
        auto msg = _queue.front();
        _queue.removeFront();
        if (msg !is null)
        {
          publishUnreceivedMessageRES(msg);
        }


        _mutex.unlock();
      }
      while(true);
  }

  void pushJob(Msg msg)
  {
    _mutex.lock();

    _queue.insertBack(msg);
    _condition.notify();
    _mutex.unlock();
  }

  private Condition           _condition;
  private Mutex               _mutex;
  private DList!(Msg)         _queue;

}

